class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def preorderTraversal(self, root: TreeNode) -> list:
        self.res = []
        if root:
            self.search(root)
        return self.res

    def search(self, root: TreeNode):
        if root.left:
            self.search(root.left)
        self.res.append(root.val)
        if root.right:
            self.search(root.right)
